/**
 * Created with JetBrains WebStorm.
 * Date: 17/11/20
 * Time: 下午1:51
 * Desc: 新闻列表.
 */
import NewsTpl from './product.html';
import _ from 'underscore';
import Util from '../../../common-component/util/util.js';
import Table from '../../../common-component/table/table.js';
import API from '../../../api/Api.js';
import Dialog from '../../../common-component/dialog/dialog.js'
import commonModal from '../../service-common/service-common.js';
import Pagination from '../../../common-component/pagination/pagination.js';
import ChoiceDialog from '../../../common-component/choice-dialog/choice-dialog.js';

import "./product.scss";

export default function DotInformation() {

    $("#app-container").html( NewsTpl({name: '新闻模块'}) );
    var tHeader = [
        {CHECKBOX:true,name:'employeeId'},
        {title:'序号',INDEX:true}
        , {title: '标题', width: '120px', name: 'title', handler: function(str){
            return str;
        }}
        , {title: '访问量', name:  'PV', handler: function(str){
            return str;
        }}
        , {title: '更新日期', name:  'updateTime', handler: function(str){
            return str;
        }}
        , {title: '状态', name:  'status', handler: function(str){
            return str;
        }}
        , {title: '排序', name:  'index',  handler: function(str){
            return str;
        }},
        {title: '操作', width: '100px', hasFunc: true
            , funcList: [{name:'编辑', op: 'update', cls: 'news-update'}
            ,{name:'下架', op: 'stop', cls: 'news-Shelf'}
            ,{name:'置顶', op: 'start', cls: 'news-Top'}
            ,{name:'推荐', op: 'review', cls: 'news-Rec'}
            ,{name: '删除', op: 'subReview', cls: 'news-del'}]}
    ];
    var data = [{
        title:'嗨皮',
        PV: '23',
        updateTime: '2017-11-22',
        status: '下架',
        index: '0',
    },{
        title:'嗨皮1',
        PV: '231',
        updateTime: '2017-11-22',
        status: '置顶',
        index: '1',
    },{
        title:'嗨皮2',
        PV: '232',
        updateTime: '2017-11-22',
        status: '推荐',
        index: '2',
    },{
        title:'嗨皮3',
        PV: '233',
        updateTime: '2017-11-22',
        status: '推荐',
        index: '3',
    },{
        title:'嗨皮4',
        PV: '234',
        updateTime: '2017-11-22',
        status: '下架',
        index: '4',
    },];

    Table($(".news-table"),'news-table',{
        tHeader: tHeader,
        data: data
    });

    const handlers = {
        checkedList: null,
        isAjax: false,
        querySearch: null,
        params: {
            pageNO: 1,
            pageSize: 10
        },
        init: function(evt) {
            const _this = this;
            this.queryForm().bindEvent();
         /*   this.querySearch = Pagination('#pagination', {
                pageSize: 10,
                url: API.queryEmployee,
                data: _this.params,
                type: 'get',
                changeParam: function(setParam, pageNO, pageSize) {
                    _this.params.pageNO = pageNO;
                    _this.params.pageSize = pageSize;
                    setParam(_this.params);
                },
                success: function(json) {
                    _this.checkedList = Table($(".news-table"), 'news-table', {
                        tHeader: tHeader,
                        data: json.list,
                        opFunction: (obj) => {
                            _this.FormButton(obj);
                        }
                    },() => {
                        $(".news-table").colResizable({
                            minWidth: 100,
                            selfResize: true
                        });
                    });
                },
                error: function() {

                },
                complete: function() {

                }
            })*/
        },
        /*
         * 表单按钮操作
         * */
        FormButton: function (e) {
            console.log(e)
            switch(e.op)
            {
                case 'delete'://删除
                    window.location.href = '#/dot-personnel-detail?id='+e.obj.employeeId
                    break;
            }
        },
        /**
         * 绑定事件
         */
        bindEvent: function() {
            const _this = this;
            $("body").on('click', '.js-handle', function(e) {
                const $this = $(this), handle = $this.data('handle');
                _this[handle] && _this[handle]($this, e);
            })
                .on('click', '.main-data', function() {
                    const $this = $(this), idx = $this.index();
                    if(_this.isAjax) {
                        return;
                    }
                    $this.addClass('active').siblings().removeClass('active');
                });
            return this;
        },
        /**
         * 表单
         */
        queryForm: function() {
            var v = $('#query_form').easyform(),_this = this;
            v.success = function (ef){//验证成功
                _this.params = $(ef.form).serializeObject(_this.params);
                _this.params.pageNO = 1;
                _this.querySearch(_this.params);
            };
            return this;
        },
        /**
         * 清除
         */
        clearForm: function() {
            let queryForm = $('#query_form'), _this = this;// 序列化表单内容
            queryForm[0].reset();
            this.params = {pageNO:1,pageSize:10};
            this.querySearch(this.params);
        },
        /**
         * 处理数据
         */
        processingData: function(data=[]) {
            data.map((item)=>{
                console.log(item)
            })
        },
        /**
         * 获取数据
         * @param 获取后的回调
         */
        getItems: function(cawllback) {
            this.ajaxHandle();
        },
        /**
         * ajax统一处理
         */
        ajaxHandle: function() {

        },
        /**
         * 统一提示语
         * @param msg 提示消息 string
         */
        tips: function(msg) {
            Util.alertMessage(msg);
        },
        /**
         * 获取选中数据
         */
        getTableCheckedData: function(){
            if(!this.checkedList) {
                return;
            }
            return this.checkedList();
        },
        /**
         * 导出
         */
        export: function(){
            // console.log(this.params)
            window.open(Util.exportParamFormat(API.exportEmployee,[]));
        },
        /*
        * 创建分类
        * */
        addNews: function () {
        var dialog = new Dialog({title:'创建新闻分类',content:'<div class="dot-personnel-dialog">' +
        '<p><label>分类名称：</label>' +
        '<input type="text" name="policyId" id="policyId" class="" placeholder="当前默认前缀unallocated"/> </p> ' +
        '</div>',button:['保存','取消'],width:500,
            noClose:true,
            confirm:function(evt){
                var policyId = $("#policyId").val();
                if(!policyId){
                    Util.alertMessage('请输入分类名称！')
                    return
                }
                $.ajax({
                    url: API.contractPolicy,
                    type: 'post',
                    data:{policyId:policyId,ids:ids},
                    success(data) {
                        evt.remove();
                        if(data.success){
                            Util.alertMessage('操作成功！');
                            _this.querySearch(_this.params);
                        }else{
                            Util.alertMessage(data.error)
                        }
                    }
                });
            }});
        }
    }

    handlers.init();

    var v = $('#query_form').easyform();
    v.success = function (ef){//验证成功
        handlers.params = $(ef.form).serializeObject(handlers.params);
        handlers.params.pageNO = 1;
        handlers.querySearch(handlers.params);
    }
    $('.resetBtn').on('click',function(){
        $('input[name=addressCode],input[name=companyCode]').val('');
        window.setTimeout(function(){
            handlers.params = {pageNO:1,pageSize:10};
            handlers.querySearch(handlers.params);
        },500);
    });

}